package com.wx.plugin.mvc;

import com.wx.plugin.context.ThreadContext;
import com.wx.plugin.mp.core.OrderRequestVO;
import com.wx.plugin.mp.core.WXAPI;
import com.wx.plugin.repository.MembersRepository;
import com.wx.plugin.sms.holder.SMSHolder;
import com.yangzcp.userpojo.Members;
import io.github.elkan1788.mpsdk4j.api.WechatAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class WebRestController {

    @Autowired
    private WechatAPI wechatAPI;

    @Autowired
    private SMSHolder smsHolder;

    @Autowired
    private WXAPI wxapi;

    @Autowired
    private MembersRepository membersRepository;

    @RequestMapping(value = "/send_yzm_do", method = RequestMethod.POST)
    String send_yzm_do(@RequestParam(value = "tel", required = true) String tel, @RequestParam(value = "sign", required = true) String sign) {
        String sessionSign = (String) ThreadContext.getRequest().getSession().getAttribute(ThreadContext.KEY_SIGN);
        if(sign.equals(sessionSign)){
            Members members = membersRepository.findByTel(tel);
            if(members != null){
                return "tel";
            }
            ThreadContext.getRequest().getSession().removeAttribute(ThreadContext.KEY_SIGN);
            smsHolder.sendYZM(tel);
            return "success";
        }else {
            return "error";
        }
    }

    @RequestMapping(value = "/check_yzm_do", method = RequestMethod.POST)
    String send_yzm_do(@RequestParam(value = "yzm", required = true) String yzm) {
        String sessionYzm = (String) ThreadContext.getRequest().getSession().getAttribute(ThreadContext.KEY_YZM);
        if(yzm.equals(sessionYzm)){
            return "success";
        }else {
            return "error";
        }
    }

    @RequestMapping(value = "/getOrder/{orderid}", method = RequestMethod.GET)
    String getOrder(@PathVariable(value = "orderid") String orderid) {
        OrderRequestVO orderRequestVO = new OrderRequestVO();
        orderRequestVO.setOrder_id(orderid);
        String order = wxapi.getOrder(orderRequestVO);
        return order;
    }

}
